<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>timer_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_timer_timer_test.js.html">timer_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.TimerTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.TimerTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line19"></a>goog.require(&#39;goog.events&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>var intervalIds = {};
<a name="line24"></a>var intervalIdCounter = 0;
<a name="line25"></a>var mockClock;
<a name="line26"></a>var maxDuration = 60 * 1000; // 60s
<a name="line27"></a>
<a name="line28"></a>function setUp() {
<a name="line29"></a>  mockClock = new goog.testing.MockClock(true /* install */);
<a name="line30"></a>}
<a name="line31"></a>
<a name="line32"></a>function tearDown() {
<a name="line33"></a>  mockClock.dispose();
<a name="line34"></a>}
<a name="line35"></a>
<a name="line36"></a>// Run a test for 60s and see how many counts we get
<a name="line37"></a>function runTest(string, ticks, number) {
<a name="line38"></a>  var t = new goog.Timer(ticks);
<a name="line39"></a>  var count = 0;
<a name="line40"></a>  goog.events.listen(t, &#39;tick&#39;, function(evt) {
<a name="line41"></a>    count++;
<a name="line42"></a>  });
<a name="line43"></a>  t.start();
<a name="line44"></a>  mockClock.tick(maxDuration);
<a name="line45"></a>  assertEquals(string, number, count);
<a name="line46"></a>  t.stop();
<a name="line47"></a>  goog.events.removeAll(t);
<a name="line48"></a>}
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>function test100msTicks() {
<a name="line52"></a>  // Desc, interval in ms, expected ticks in 60s
<a name="line53"></a>  runTest(&#39;10 ticks per second for 60 seconds&#39;, 100, 600);
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>function test500msTicks() {
<a name="line57"></a>  runTest(&#39;2 ticks per second for 60 seconds&#39;, 500, 120);
<a name="line58"></a>}
<a name="line59"></a>
<a name="line60"></a>function test1sTicks() {
<a name="line61"></a>  runTest(&#39;1 tick per second for 60 seconds&#39;, 1000, 60);
<a name="line62"></a>}
<a name="line63"></a>
<a name="line64"></a>function test2sTicks() {
<a name="line65"></a>  runTest(&#39;1 tick every 2 seconds for 60 seconds&#39;, 2000, 30);
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>function test5sTicks() {
<a name="line69"></a>  runTest(&#39;1 tick every 5 seconds for 60 seconds&#39;, 5000, 12);
<a name="line70"></a>}
<a name="line71"></a>
<a name="line72"></a>function test10sTicks() {
<a name="line73"></a>  runTest(&#39;1 tick every 10 seconds for 60 seconds&#39;, 10000, 6);
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>function test30sTicks() {
<a name="line77"></a>  runTest(&#39;1 tick every 30 seconds for 60 seconds&#39;, 30000, 2);
<a name="line78"></a>}
<a name="line79"></a>
<a name="line80"></a>function test60sTicks() {
<a name="line81"></a>  runTest(&#39;1 tick every 60 seconds&#39;, 60000, 1);
<a name="line82"></a>}
<a name="line83"></a>
<a name="line84"></a>function testCallOnce() {
<a name="line85"></a>  var c = 0;
<a name="line86"></a>  var actualTimeoutId = goog.Timer.callOnce(
<a name="line87"></a>      function() {
<a name="line88"></a>        if (c &gt; 0) {
<a name="line89"></a>          assertTrue(&#39;callOnce should only be called once&#39;, false);
<a name="line90"></a>        }
<a name="line91"></a>        c++;
<a name="line92"></a>      });
<a name="line93"></a>  assertEquals(&#39;callOnce should return the timeout ID&#39;,
<a name="line94"></a>      mockClock.getTimeoutsMade(), actualTimeoutId);
<a name="line95"></a>
<a name="line96"></a>  var obj = {c: 0};
<a name="line97"></a>  goog.Timer.callOnce(function() {
<a name="line98"></a>    if (this.c &gt; 0) {
<a name="line99"></a>      assertTrue(&#39;callOnce should only be called once&#39;, false);
<a name="line100"></a>    }
<a name="line101"></a>    assertEquals(obj, this);
<a name="line102"></a>    this.c++;
<a name="line103"></a>  }, 1, obj);
<a name="line104"></a>  mockClock.tick(maxDuration);
<a name="line105"></a>}
<a name="line106"></a>
<a name="line107"></a>function testCallOnceIgnoresTimeoutsTooLarge() {
<a name="line108"></a>  var failCallback = goog.partial(fail, &#39;Timeout should never be called&#39;);
<a name="line109"></a>  assertEquals(&#39;Timeouts slightly too large should yield a timer ID of -1&#39;,
<a name="line110"></a>      -1, goog.Timer.callOnce(failCallback, 2147483648));
<a name="line111"></a>  assertEquals(&#39;Infinite timeouts should yield a timer ID of -1&#39;,
<a name="line112"></a>      -1, goog.Timer.callOnce(failCallback, Infinity));
<a name="line113"></a>}
</pre>


</body>
</html>
